//
//  AddSchoolViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/17.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "AddSchoolViewController.h"
#import "loginTextField.h"

@interface AddSchoolViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray *textValeArray;
@end

@implementation AddSchoolViewController
- (NSMutableArray *)textValeArray{
    if (!_textValeArray) {
        _textValeArray = [NSMutableArray array];
    }
    return _textValeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加学校";
    if ([self.addSchoolType isEqualToString:@"perent"]) {
        self.title = @"添加孩子";
    }
    [self creatFootView];
    self.textValeArray = [NSMutableArray arrayWithObjects:@"", nil];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)creatFootView{
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, 0, kScreenW, 100);
    footView.backgroundColor = [UIColor whiteColor];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"添加"];
    [completeButton addTarget:self action:@selector(addSchool) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
    self.tableView.tableFooterView = footView;
}
#define mark - 添加学校
- (void)addSchool{
    [self.tableView endEditing:YES];
    if (strIsEmpty(self.textValeArray[0])) {
        [Global showActivityWithView:self.view withText:@"请输入系统码"];
        return;
    }
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//    [dic setValue:[self.textValeArray componentsJoinedByString:@","] forKey:@"sysCode"];   //学生验证码
    [dic setValue:self.textValeArray[0] forKey:@"sysCodes"];   //学生验证码
    if ([self.addSchoolType isEqualToString:@"perent"]) {
        [dic setValue:@"1" forKey:@"type"];   //绑定孩子
    }else{
        [dic setValue:@"2" forKey:@"type"];   //绑定学校
    }
    
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppBindInfo] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"绑定学校成功"];
            [self.navigationController popViewControllerAnimated:YES];
            if (self.addSchoolBlock) {
                self.addSchoolBlock();
            }
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    loginTextField *loginTF = [[loginTextField alloc]initWithFrame:CGRectMake(20, 20, kScreenW - 40 , 40) placeholder:@"输入系统注册码" imageStr:@""];
    loginTF.tag = 1000 + indexPath.row;
    loginTF.text=self.textValeArray[indexPath.row];
    loginTF.keyboardType = UIKeyboardTypeAlphabet;
    loginTF.delegate = self;
    [cell.contentView  addSubview:loginTF];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [self.textValeArray replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text];
    [textField resignFirstResponder];
    return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.textValeArray replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text];
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
